package cc.cc4414.cc02.chat.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cc.cc4414.cc02.chat.entity.ChatGroup;
import cc.cc4414.cc02.chat.entity.Friend;
import cc.cc4414.cc02.chat.entity.GroupUser;
import cc.cc4414.cc02.chat.repository.FriendRepository;
import cc.cc4414.cc02.chat.repository.GroupRepository;
import cc.cc4414.cc02.chat.repository.GroupUserRepository;
import cc.cc4414.cc02.chat.service.GroupUserService;
import cc.cc4414.cc02.core.result.ResultEnum;
import cc.cc4414.cc02.core.result.ResultException;
import cc.cc4414.cc02.system.entity.User;
import cc.cc4414.cc02.system.repository.UserRepository;

/**
 * @author cc 2018年9月7日
 */
@Service
public class GroupUserServiceImpl implements GroupUserService {

	@Autowired
	private GroupUserRepository groupUserRepository;

	@Autowired
	private FriendRepository friendRepository;

	@Autowired
	private GroupRepository groupRepository;

	@Autowired
	private UserRepository userRepository;

	@Override
	public void delete(Long groupId, Long userId) {
		GroupUser groupUser = groupUserRepository.findByGroupIdAndUserId(groupId, userId);
		if (groupUser == null) {
			throw new ResultException(ResultEnum.NOT_IN_GROUP);
		} else {
			groupUserRepository.delete(groupUser);
		}
	}

	@Override
	public void add(Long groupId, Long userId, Long id) {
		// 群必须存在
		Optional<ChatGroup> optional = groupRepository.findById(groupId);
		if (!optional.isPresent() || !optional.get().getState().equals(0)) {
			throw new ResultException(ResultEnum.GROUP_NOT_EXIST);
		}
		ChatGroup group = optional.get();
		// 如果自己不在群内，则无法将好友拉入群
		GroupUser myGroupUser = groupUserRepository.findByGroupIdAndUserId(groupId, id);
		if (myGroupUser == null) {
			throw new ResultException(ResultEnum.NOT_IN_GROUP);
		}
		// 只能拉自己的好友入群
		Friend friend = friendRepository.findByUserIdAndFriendId(id, userId);
		if (friend == null || !friend.getState().equals(1)) {
			throw new ResultException(ResultEnum.NOT_FRIEND);
		}
		User user = userRepository.findById(userId).get();
		// 对方不能已在群内
		GroupUser groupUserFriend = groupUserRepository.findByGroupIdAndUserId(groupId, userId);
		if (groupUserFriend != null) {
			throw new ResultException(ResultEnum.IN_GROUP);
		}
		GroupUser groupUser = new GroupUser();
		groupUser.setCreateTime(new Date());
		groupUser.setGroupId(groupId);
		groupUser.setGroupName(group.getGroupName());
		groupUser.setUserId(userId);
		groupUser.setUserNickname(user.getNickname());
		groupUser.setLastMessageSendId(0L);
		groupUserRepository.save(groupUser);
	}

	@Override
	public List<GroupUser> findGroupUsers(Long userId) {
		return groupUserRepository.findByUserId(userId);
	}

	@Override
	public List<GroupUser> findGroupMember(Long groupId, Long userId) {
		// 群必须存在
		Optional<ChatGroup> optional = groupRepository.findById(groupId);
		if (!optional.isPresent() || !optional.get().getState().equals(0)) {
			throw new ResultException(ResultEnum.GROUP_NOT_EXIST);
		}
		// 如果自己不在群内，则无法查询群成员
		GroupUser myGroupUser = groupUserRepository.findByGroupIdAndUserId(groupId, userId);
		if (myGroupUser == null) {
			throw new ResultException(ResultEnum.NOT_IN_GROUP);
		}
		return groupUserRepository.findByGroupId(groupId);
	}

}
